﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace BYManageInfoSystem
{
    public abstract class Fin_PayTaxBase
    {
        protected DGBY.BLL.FinCheckInvoiceRecordDetailTab_New m_bllFinCheckInvoiceRecordDetailTab_New = new DGBY.BLL.FinCheckInvoiceRecordDetailTab_New();
        protected DGBY.BLL.FactoryGoodsTab m_bllFactoryGoodsTab = new DGBY.BLL.FactoryGoodsTab();
        protected DGBY.BLL.FinRecvMoneyDetailTab_New m_bllFinRecvMoneyDetailTab_New = new DGBY.BLL.FinRecvMoneyDetailTab_New();

        protected Hashtable m_SQLStringList = new Hashtable();
        protected ArrayList m_arrayList = new ArrayList();
        protected DGBY.BLL.FinDepRecvAndPayDetailTab_New m_bllFinDepRecvAndPayDetailTab_New = new DGBY.BLL.FinDepRecvAndPayDetailTab_New();
        protected DGBY.BLL.FinRecvMoneyAuxiliaryTab_New m_bllFinRecvMoneyAuxiliaryTab_New = new DGBY.BLL.FinRecvMoneyAuxiliaryTab_New();
        protected DGBY.BLL.FinCheckInvoiceRecordDetailAuxiliaryTab_New m_bllInvoiceAuxiliary_New
         = new DGBY.BLL.FinCheckInvoiceRecordDetailAuxiliaryTab_New();

        protected abstract Decimal GetRecvAuxiliary2RecvFactor(string strRecvBianNo);
     
        protected abstract bool isAllBackTax(Decimal dtTotal, Decimal dtPayedValue, DGBY.Model.FinCheckInvoiceRecordDetailTab_New modelFinCheckInvoiceRecordDetailTab_New);
        protected abstract void ComputeCometoFacFormulaForFomularSelectZero(Dictionary<Decimal, Decimal> dicPayTaxPercent2InvoiceYuE
    , ref DGBY.Model.FinDepRecvAndPayDetailTab_New modelFinDepRecvAndPayDetailTab_New, Decimal dtThisRecv, Decimal dtThis, string strGongshi);


        protected virtual string GetFisrtFormularValue(string strGongshi, Decimal dtThisRecv)
        {
            string strFisrtValue = "";
            if (strGongshi == "")
                strFisrtValue = dtThisRecv.ToString("0.00");
            else
                strFisrtValue = " ( " + dtThisRecv.ToString("0.00")
    + strGongshi + " ) ";

            return strFisrtValue;
        }

        protected virtual void ComputeCometoFacFormulaForFomularSelectOne(Dictionary<Decimal, Decimal> dicPayTaxPercent2InvoiceYuE
           , ref DGBY.Model.FinDepRecvAndPayDetailTab_New modelFinDepRecvAndPayDetailTab_New, Decimal dtThisRecv, Decimal dtThis, string strGongshi)
        {
            Decimal dtToFacMoneyTemp = 0;
            string strPayTaxBenJinGongShiTemp = "";
            string strFisrtValue = GetFisrtFormularValue(strGongshi, dtThisRecv);
            foreach (Decimal dtBackTaxPoint in dicPayTaxPercent2InvoiceYuE.Keys)
            {
                Decimal dtPayBenJin = dicPayTaxPercent2InvoiceYuE[dtBackTaxPoint];
                if (dtBackTaxPoint == modelFinDepRecvAndPayDetailTab_New.FactorB)
                {
                    dtToFacMoneyTemp = dtPayBenJin
* modelFinDepRecvAndPayDetailTab_New.HuiLv * modelFinDepRecvAndPayDetailTab_New.FactorA - dtPayBenJin
* modelFinDepRecvAndPayDetailTab_New.HuiLv;

                    

                    if(Math.Abs(dtThis - dtPayBenJin) < 0.5M)
                    {
                        strPayTaxBenJinGongShiTemp =
                  strFisrtValue + " * "
                  + modelFinDepRecvAndPayDetailTab_New.HuiLv.ToString() + " * " + modelFinDepRecvAndPayDetailTab_New.FactorA.ToString()
                  + " - " + dtPayBenJin.ToString("0.00") + " * "
                  + modelFinDepRecvAndPayDetailTab_New.HuiLv.ToString() + " * 1";
                    }
                    else
                    {
                        strPayTaxBenJinGongShiTemp =
                  " ( " + dtThisRecv.ToString("0.00")
+ strGongshi + " - " + (dtThis - dtPayBenJin).ToString("0.00") + " ) " + " * "
                  + modelFinDepRecvAndPayDetailTab_New.HuiLv.ToString() + " * " + modelFinDepRecvAndPayDetailTab_New.FactorA.ToString()
                  + " - " + dtPayBenJin.ToString("0.00") + " * "
                  + modelFinDepRecvAndPayDetailTab_New.HuiLv.ToString() + " * 1";
                    }
             
                }
                else
                {
                    dtToFacMoneyTemp = dtPayBenJin
* modelFinDepRecvAndPayDetailTab_New.HuiLv * (modelFinDepRecvAndPayDetailTab_New.FactorA + dtBackTaxPoint
- (modelFinDepRecvAndPayDetailTab_New.FactorB)) - dtPayBenJin
* modelFinDepRecvAndPayDetailTab_New.HuiLv;


                    if (Math.Abs(dtThis - dtPayBenJin) < 0.5M)
                    {
                        strPayTaxBenJinGongShiTemp =
                        strFisrtValue + " * "
                        + modelFinDepRecvAndPayDetailTab_New.HuiLv.ToString() + " * " + (modelFinDepRecvAndPayDetailTab_New.FactorA
                        + dtBackTaxPoint - (modelFinDepRecvAndPayDetailTab_New.FactorB)).ToString()
                        + " - " + dtPayBenJin.ToString("0.00") + " * "
                + modelFinDepRecvAndPayDetailTab_New.HuiLv.ToString() + " * 1";
                    }
                    else
                    {
                        strPayTaxBenJinGongShiTemp =
                         " ( " + dtThisRecv.ToString("0.00")
+ strGongshi + " - " + (dtThis - dtPayBenJin).ToString("0.00") + " ) " + " * "
                         + modelFinDepRecvAndPayDetailTab_New.HuiLv.ToString() + " * " + (modelFinDepRecvAndPayDetailTab_New.FactorA
                         + dtBackTaxPoint - (modelFinDepRecvAndPayDetailTab_New.FactorB)).ToString()
                         + " - " + dtPayBenJin.ToString("0.00") + " * "
                 + modelFinDepRecvAndPayDetailTab_New.HuiLv.ToString() + " * 1";
                    }
                   
                }

                modelFinDepRecvAndPayDetailTab_New.ToFacMoney += dtToFacMoneyTemp;

                if (modelFinDepRecvAndPayDetailTab_New.PayTaxBenJinGongShi == null)
                {
                    modelFinDepRecvAndPayDetailTab_New.PayTaxBenJinGongShi
                        = strPayTaxBenJinGongShiTemp;
                }
                else
                {
                    modelFinDepRecvAndPayDetailTab_New.PayTaxBenJinGongShi
                        += " + " + strPayTaxBenJinGongShiTemp;
                }
            }
        }

        public Fin_PayTaxBase()
        {
        }

        public Fin_PayTaxBase(Hashtable SQLStringList, ArrayList arrayList)
        {
            m_SQLStringList = SQLStringList;
            m_arrayList = arrayList;
        }

        protected virtual Decimal GetHkHuiLv(DGBY.Model.FinDepRecvAndPayDetailTab_New modelFinDepRecvAndPayDetailTab_New)
        {
            Decimal dtHkRecvHuiLv = 0;

            DataSet ds = m_bllFinRecvMoneyDetailTab_New.GetList("编号 = '" + modelFinDepRecvAndPayDetailTab_New.RecvBianNo + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {


                dtHkRecvHuiLv = (Decimal)ds.Tables[0].Rows[0]["港币汇率"];
            }

            return dtHkRecvHuiLv;
        }

        protected virtual string GetHuiLvFormular(DGBY.Model.FinDepRecvAndPayDetailTab_New modelFinDepRecvAndPayDetailTab_New)
        {
            string strRecvHuiLv = "";
           
            DataSet ds = m_bllFinRecvMoneyDetailTab_New.GetList("编号 = '" + modelFinDepRecvAndPayDetailTab_New.RecvBianNo + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                

                strRecvHuiLv = ds.Tables[0].Rows[0]["汇率公式"].ToString();
            }

            return strRecvHuiLv;
        }
        public void ProcessPayTaxOnRecvMoneyTab(DGBY.Model.FinDepRecvAndPayDetailTab_New modelFinDepRecvAndPayDetailTab_New)
        {
            Decimal dtFactor = GetRecvAuxiliary2RecvFactor(modelFinDepRecvAndPayDetailTab_New.RecvBianNo);
            DataSet dsTemp = m_bllFinRecvMoneyDetailTab_New.GetList("z1.不参与退税 = 0 and 编号 = '" + modelFinDepRecvAndPayDetailTab_New.RecvBianNo + "'");
            //Decimal dtHuiLv = 0;
            //if (dsTemp.Tables.Count > 0 && dsTemp.Tables[0].Rows.Count > 0)
            //{
            //    dtHuiLv = Convert.ToDecimal(dsTemp.Tables[0].Rows[0]["汇率"].ToString());
            //}

            //Decimal dtPayTaxBenJin = modelFinDepRecvAndPayDetailTab_New.PayTaxBenJin * dtHuiLv;
            Decimal dtPayTaxBenJin = modelFinDepRecvAndPayDetailTab_New.PayTaxBenJin * dtFactor;
            //Decimal dtPayTaxBenJin = modelFinDepRecvAndPayDetailTab_New.PayTaxBenJin;
            DataSet ds = m_bllFinRecvMoneyAuxiliaryTab_New.GetRecvMoneyEnumRecord(@"FinRecvMoneyAuxiliaryTab_New.IsPayTaxDone = 0 
                and FinRecvMoneyDetailTab_New.BianHao = '" + modelFinDepRecvAndPayDetailTab_New.RecvBianNo
             + "' and FinRecvMoneyDetailTab_New.FactoryDataTabID = '"
                + modelFinDepRecvAndPayDetailTab_New.FactoryDataTabID.ToString() + "'");

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (dtPayTaxBenJin == 0)
                        break;
                    int nFinRecvMoneyAuxiliaryTab_NewID = Convert.ToInt32(ds.Tables[0].Rows[i]["FinRecvMoneyAuxiliaryTab_NewID"].ToString(), 10);
                    Decimal dtTemp = Convert.ToDecimal(ds.Tables[0].Rows[i]["PayTaxMoneyValue"].ToString());
                    if (dtTemp <= dtPayTaxBenJin)
                    {
                        dtPayTaxBenJin -= dtTemp;
                        DGBY.Model.FinRecvMoneyAuxiliaryTab_New modelFinRecvMoneyAuxiliaryTab_New = m_bllFinRecvMoneyAuxiliaryTab_New.GetModel(nFinRecvMoneyAuxiliaryTab_NewID);
                        if (modelFinRecvMoneyAuxiliaryTab_New.SubSeqNo == 0)
                        {
                            DGBY.Model.FinRecvMoneyDetailTab_New modelFinRecvMoneyDetailTab_New
                                = m_bllFinRecvMoneyDetailTab_New.GetModel(modelFinRecvMoneyAuxiliaryTab_New.FinRecvMoneyDetailTab_NewID);
                            modelFinRecvMoneyDetailTab_New.IsPayTaxDone = 1;
                            m_bllFinRecvMoneyDetailTab_New.TransUpdate(modelFinRecvMoneyDetailTab_New
                                , m_SQLStringList, m_arrayList, i);
                        }

                        modelFinRecvMoneyAuxiliaryTab_New.IsPayTaxDone = 1;
                        modelFinRecvMoneyAuxiliaryTab_New.PayedTaxMoneyValue = dtTemp;
                        modelFinRecvMoneyAuxiliaryTab_New.PayTaxBianHao = modelFinDepRecvAndPayDetailTab_New.FinDepRecvAndPayDetailTab_NewBianNo;
                        m_bllFinRecvMoneyAuxiliaryTab_New.TransUpdate(modelFinRecvMoneyAuxiliaryTab_New
                            , m_SQLStringList, m_arrayList, i);
                    }
                    else
                    {
                        DGBY.Model.FinRecvMoneyAuxiliaryTab_New modelFinRecvMoneyAuxiliaryTab_New = m_bllFinRecvMoneyAuxiliaryTab_New.GetModel(nFinRecvMoneyAuxiliaryTab_NewID);
                        int nFinRecvMoneyDetailTab_NewID = modelFinRecvMoneyAuxiliaryTab_New.FinRecvMoneyDetailTab_NewID;
                        if (modelFinRecvMoneyAuxiliaryTab_New.SubSeqNo == 0)
                        {
                            DGBY.Model.FinRecvMoneyDetailTab_New modelFinRecvMoneyDetailTab_New
                                = m_bllFinRecvMoneyDetailTab_New.GetModel(modelFinRecvMoneyAuxiliaryTab_New.FinRecvMoneyDetailTab_NewID);
                            modelFinRecvMoneyDetailTab_New.IsPayTaxDone = 1;
                            m_bllFinRecvMoneyDetailTab_New.TransUpdate(modelFinRecvMoneyDetailTab_New
                                , m_SQLStringList, m_arrayList, i);
                        }
                        Decimal dtTemp2 = dtTemp - dtPayTaxBenJin;
                        if (dtTemp2 < (Decimal)0.01)
                        {
                            dtPayTaxBenJin = dtTemp;
                        }
                        modelFinRecvMoneyAuxiliaryTab_New.IsPayTaxDone = 1;
                        modelFinRecvMoneyAuxiliaryTab_New.PayTaxBianHao = modelFinDepRecvAndPayDetailTab_New.FinDepRecvAndPayDetailTab_NewBianNo;
                        modelFinRecvMoneyAuxiliaryTab_New.PayedTaxMoneyValue = dtPayTaxBenJin;
                        m_bllFinRecvMoneyAuxiliaryTab_New.TransUpdate(modelFinRecvMoneyAuxiliaryTab_New
                            , m_SQLStringList, m_arrayList, i);

                        if (dtTemp2 >= (Decimal)0.01)
                        {
                            dtTemp2 = dtTemp - dtPayTaxBenJin;
                            dtPayTaxBenJin = 0;

                            DGBY.Model.FinRecvMoneyAuxiliaryTab_New modelFinRecvMoneyAuxiliaryTab_New2 = new DGBY.Model.FinRecvMoneyAuxiliaryTab_New();
                            modelFinRecvMoneyAuxiliaryTab_New2.FinRecvMoneyDetailTab_NewID = nFinRecvMoneyDetailTab_NewID;
                            modelFinRecvMoneyAuxiliaryTab_New2.IsPayTaxDone = 0;
                            modelFinRecvMoneyAuxiliaryTab_New2.PayTaxBianHao = modelFinDepRecvAndPayDetailTab_New.FinDepRecvAndPayDetailTab_NewBianNo;
                            modelFinRecvMoneyAuxiliaryTab_New2.PayTaxMoneyValue = dtTemp2;
                            modelFinRecvMoneyAuxiliaryTab_New.PayedTaxMoneyValue = 0;
                            modelFinRecvMoneyAuxiliaryTab_New2.PayTaxDate = System.DateTime.Now.Date;
                            modelFinRecvMoneyAuxiliaryTab_New2.SubSeqNo = modelFinRecvMoneyAuxiliaryTab_New.SubSeqNo + 1;
                            m_bllFinRecvMoneyAuxiliaryTab_New.TransAdd(modelFinRecvMoneyAuxiliaryTab_New2
                                , m_SQLStringList, m_arrayList, i);
                        }

                    }


                }
            }

        }
        protected Decimal GetInvoiceAuxiliaryTotalTaxJineByID(int nFinCheckInvoiceRecordDetailTab_NewID)
        {
            Decimal dtTotal = 0;
            DataSet ds = m_bllInvoiceAuxiliary_New.GetCheckInvoiceRecordDetailAuxiliaryInfo("FinCheckInvoiceRecordDetailTab_New.FinCheckInvoiceRecordDetailTab_NewID = '" + nFinCheckInvoiceRecordDetailTab_NewID.ToString() + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    Decimal dtTemp = Convert.ToDecimal(ds.Tables[0].Rows[i]["已付退税值"].ToString());
                    dtTotal += dtTemp;
                }
            }

            return dtTotal;
        }
        protected void PartBackTaxUpdateRecord(int nFinCheckInvoiceRecordDetailAuxiliaryTab_NewID
         , int nFinCheckInvoiceRecordDetailTab_NewID, int nIndex, Decimal dtPayedValue, Decimal dtNextPayedValue, string PayTaxBianHao)
        {
            DGBY.Model.FinCheckInvoiceRecordDetailAuxiliaryTab_New modelFinCheckInvoiceRecordDetailAuxiliaryTab_New
                     = m_bllInvoiceAuxiliary_New.GetModel(nFinCheckInvoiceRecordDetailAuxiliaryTab_NewID);
            //int nFinCheckInvoiceRecordDetailTab_NewID
            //    = modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.FinCheckInvoiceRecordDetailTab_NewID;

            modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.IsPayTaxDone = 1;
            modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.PayedTaxMoneyValue = dtPayedValue;
            modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.PayTaxBianHao = PayTaxBianHao;
            modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.PayTaxDate = System.DateTime.Now.Date;
            m_bllInvoiceAuxiliary_New.TransUpdate(modelFinCheckInvoiceRecordDetailAuxiliaryTab_New
                , m_SQLStringList, m_arrayList, nIndex);

            //Decimal dtTemp2 = dtTemp - dtPayedValue;

            if (dtNextPayedValue > 0)
            {
                DGBY.Model.FinCheckInvoiceRecordDetailAuxiliaryTab_New modelFinCheckInvoiceRecordDetailAuxiliaryTab_New2
                    = new DGBY.Model.FinCheckInvoiceRecordDetailAuxiliaryTab_New();
                modelFinCheckInvoiceRecordDetailAuxiliaryTab_New2.FinCheckInvoiceRecordDetailTab_NewID = nFinCheckInvoiceRecordDetailTab_NewID;
                modelFinCheckInvoiceRecordDetailAuxiliaryTab_New2.IsPayTaxDone = 0;
                // modelFinCheckInvoiceRecordDetailAuxiliaryTab_New2.PayTaxBianHao = modelFinDepRecvAndPayDetailTab_New.FinDepRecvAndPayDetailTab_NewBianNo;
                modelFinCheckInvoiceRecordDetailAuxiliaryTab_New2.PayTaxMoneyValue = dtNextPayedValue;
                modelFinCheckInvoiceRecordDetailAuxiliaryTab_New2.PayedTaxMoneyValue = 0;
                modelFinCheckInvoiceRecordDetailAuxiliaryTab_New2.PayTaxPercent = modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.PayTaxPercent;

                // modelFinCheckInvoiceRecordDetailAuxiliaryTab_New2.PayTaxDate = System.DateTime.Now.Date;
                modelFinCheckInvoiceRecordDetailAuxiliaryTab_New2.SubSeqNo = modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.SubSeqNo + 1;
                m_bllInvoiceAuxiliary_New.TransAdd(modelFinCheckInvoiceRecordDetailAuxiliaryTab_New2
                    , m_SQLStringList, m_arrayList, nIndex);
            }
        }
        
     

        protected void AllBackTaxUpdateRecord(int nFinCheckInvoiceRecordDetailAuxiliaryTab_NewID
            , int nFinCheckInvoiceRecordDetailTab_NewID, int nIndex, Decimal dtPayedValue, string PayTaxBianHao)
        {
            DGBY.Model.FinCheckInvoiceRecordDetailAuxiliaryTab_New modelFinCheckInvoiceRecordDetailAuxiliaryTab_New
                            = m_bllInvoiceAuxiliary_New.GetModel(nFinCheckInvoiceRecordDetailAuxiliaryTab_NewID);

            Decimal dtTotal = GetInvoiceAuxiliaryTotalTaxJineByID(modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.FinCheckInvoiceRecordDetailTab_NewID);

            DGBY.Model.FinCheckInvoiceRecordDetailTab_New modelFinCheckInvoiceRecordDetailTab_New
                = m_bllFinCheckInvoiceRecordDetailTab_New.GetModel(modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.FinCheckInvoiceRecordDetailTab_NewID);
            if (isAllBackTax(dtTotal, dtPayedValue, modelFinCheckInvoiceRecordDetailTab_New))
            {

                modelFinCheckInvoiceRecordDetailTab_New.IsPayTaxDone = 1;
                m_bllFinCheckInvoiceRecordDetailTab_New.TransUpdate(modelFinCheckInvoiceRecordDetailTab_New
                    , m_SQLStringList, m_arrayList, nIndex);
            }

            modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.IsPayTaxDone = 1;
            modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.PayedTaxMoneyValue = dtPayedValue;
            modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.PayTaxBianHao = PayTaxBianHao;// 
            modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.PayTaxDate = System.DateTime.Now.Date;
            m_bllInvoiceAuxiliary_New.TransUpdate(modelFinCheckInvoiceRecordDetailAuxiliaryTab_New
                , m_SQLStringList, m_arrayList, nIndex);
        }
        protected bool BackTaxStep(string strWhere, ref int nAccuIndex, ref Decimal dtPayTaxBenJin
           , ref Dictionary<Decimal, Decimal> dicPayTaxPercent2InvoiceYuE
           , DGBY.Model.FinDepRecvAndPayDetailTab_New modelFinDepRecvAndPayDetailTab_New)
        {
            Decimal dtFactor = GetRecvAuxiliary2RecvFactor(modelFinDepRecvAndPayDetailTab_New.RecvBianNo);
            bool bFinishBackTax = false;
            DataSet ds = m_bllInvoiceAuxiliary_New.GetInvoiceEnumRecord(strWhere);

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    nAccuIndex++;
                    int nFinCheckInvoiceRecordDetailAuxiliaryTab_NewID = (int)ds.Tables[0].Rows[i]["FinCheckInvoiceRecordDetailAuxiliaryTab_NewID"];
                    int nFinCheckInvoiceRecordDetailTab_NewID = (int)ds.Tables[0].Rows[i]["FinCheckInvoiceRecordDetailTab_NewID"];
                    Decimal dtPayTaxPercent = Convert.ToDecimal(ds.Tables[0].Rows[i]["PayTaxPercent"].ToString());
                    Decimal dtTemp = Convert.ToDecimal(ds.Tables[0].Rows[i]["InvoiceYuE"].ToString()) / dtFactor;
                    Decimal dtTrueTemp = 0;

                    if (dtPayTaxBenJin == 0)
                    {
                        bFinishBackTax = true;
                        break;
                    }

                    if (dtTemp <= dtPayTaxBenJin)
                    {
                        dtPayTaxBenJin -= dtTemp;
                        dtTrueTemp = dtTemp;

                        AllBackTaxUpdateRecord(nFinCheckInvoiceRecordDetailAuxiliaryTab_NewID
                            , nFinCheckInvoiceRecordDetailTab_NewID, nAccuIndex, dtTemp * dtFactor
                            , modelFinDepRecvAndPayDetailTab_New.FinDepRecvAndPayDetailTab_NewBianNo);
                    }
                    else
                    {
                        dtTrueTemp = dtPayTaxBenJin;
                        dtPayTaxBenJin = 0;
                        PartBackTaxUpdateRecord(nFinCheckInvoiceRecordDetailAuxiliaryTab_NewID
                            , nFinCheckInvoiceRecordDetailTab_NewID, nAccuIndex, dtTrueTemp * dtFactor, (dtTemp - dtTrueTemp) * dtFactor
                            , modelFinDepRecvAndPayDetailTab_New.FinDepRecvAndPayDetailTab_NewBianNo);
                    }


                    if (!dicPayTaxPercent2InvoiceYuE.Keys.Contains(dtPayTaxPercent))
                        dicPayTaxPercent2InvoiceYuE.Add(dtPayTaxPercent, dtTrueTemp);
                    else
                    {
                        Decimal dtTemp2 = dicPayTaxPercent2InvoiceYuE[dtPayTaxPercent];
                        dicPayTaxPercent2InvoiceYuE[dtPayTaxPercent] = dtTemp2 + dtTrueTemp;
                    }

                }

                if (dtPayTaxBenJin == 0)
                {
                    bFinishBackTax = true;
                }
            }

            return bFinishBackTax;
        }
        protected Dictionary<Decimal, Decimal> ComputeComeToFacMoneyDiffBackTax(int nFomularSelect, DGBY.Model.FinDepRecvAndPayDetailTab_New modelFinDepRecvAndPayDetailTab_New)
        {
            //ArrayList al = new ArrayList();

            Decimal dtBackTaxPoint = 0;
            DataSet dsInfo = m_bllFactoryGoodsTab.GetGoodsBandInfo("FactoryDataTab.FactoryDataTabID = '"
                + modelFinDepRecvAndPayDetailTab_New.FactoryDataTabID.ToString() + "'");
            if (dsInfo.Tables.Count > 0 && dsInfo.Tables[0].Rows.Count > 0)
            {
                if(nFomularSelect == 0)
                    dtBackTaxPoint = 1.17m - Convert.ToDecimal(dsInfo.Tables[0].Rows[0]["退税点"].ToString());
                else
                    dtBackTaxPoint = 1.17m - Convert.ToDecimal(dsInfo.Tables[0].Rows[0]["设百分比"].ToString());
            }

            Decimal dtPayTaxBenJin = modelFinDepRecvAndPayDetailTab_New.PayTaxBenJin;
            //Decimal dtPayTaxBenJin = modelFinDepRecvAndPayDetailTab_New.PayTaxBenJin;
            //1.找到上次动过的最后一个审票
            DataSet ds = m_bllInvoiceAuxiliary_New.GetInvoiceLastBackTaxRecord(@"FinCheckInvoiceRecordDetailAuxiliaryTab_New.IsPayTaxDone > 0
 and FactoryDataTab.FactoryDataTabID = '"
                + modelFinDepRecvAndPayDetailTab_New.FactoryDataTabID.ToString() + "'");

            string strLastBianNo = "";//上次动过的最后一个审票

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                strLastBianNo = ds.Tables[0].Rows[0]["maxBianHao"].ToString();
                if (strLastBianNo == "")//该工厂还没有退过审票
                {
                    ds = m_bllInvoiceAuxiliary_New.GetInvoiceFirstBackTaxRecord(@"FactoryDataTab.FactoryDataTabID = '"
        + modelFinDepRecvAndPayDetailTab_New.FactoryDataTabID.ToString() + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        strLastBianNo = ds.Tables[0].Rows[0]["maxBianHao"].ToString();
                    }
                }
            }
            else
            {
                //不存在，顶多maxBianHao==null
            }
            Dictionary<Decimal, Decimal> dicPayTaxPercent2InvoiceYuE = new Dictionary<Decimal, Decimal>();
            int nAccuIndex = 0;
            bool bFinishBackTax = false;

            //1.先退上次动过的，及上次动过之前的审票（做为一个整体考察）,(1)退税点不等于正常退税点的	
            bFinishBackTax = BackTaxStep(@"FinCheckInvoiceRecordDetailTab_New.BianHao <= '"
                + strLastBianNo + @"' and FinCheckInvoiceRecordDetailAuxiliaryTab_New.IsPayTaxDone = 0 and PayTaxPercent != '" + dtBackTaxPoint
                + "' and FactoryDataTab.FactoryDataTabID = '" + modelFinDepRecvAndPayDetailTab_New.FactoryDataTabID.ToString() + "'"
                , ref nAccuIndex, ref dtPayTaxBenJin, ref dicPayTaxPercent2InvoiceYuE, modelFinDepRecvAndPayDetailTab_New);

            if (bFinishBackTax)
            {
                return dicPayTaxPercent2InvoiceYuE;
            }
            else
            {
                //1.先退上次动过的，及上次动过之前的审票（做为一个整体考察）,(2)退税点等于正常退税点的	
                bFinishBackTax = BackTaxStep(@"FinCheckInvoiceRecordDetailTab_New.BianHao <= '"
                 + strLastBianNo + @"' and FinCheckInvoiceRecordDetailAuxiliaryTab_New.IsPayTaxDone = 0 and PayTaxPercent = '" + dtBackTaxPoint
                 + "' and FactoryDataTab.FactoryDataTabID = '" + modelFinDepRecvAndPayDetailTab_New.FactoryDataTabID.ToString() + "'"
                 , ref nAccuIndex, ref dtPayTaxBenJin, ref dicPayTaxPercent2InvoiceYuE, modelFinDepRecvAndPayDetailTab_New);

                if (bFinishBackTax)
                {
                    return dicPayTaxPercent2InvoiceYuE;
                }
                else
                {
                    bFinishBackTax = BackTaxStep(@"FinCheckInvoiceRecordDetailTab_New.BianHao > '"
                                   + strLastBianNo + @"' and FinCheckInvoiceRecordDetailAuxiliaryTab_New.IsPayTaxDone = 0 and PayTaxPercent != '" + dtBackTaxPoint
                                   + "' and FactoryDataTab.FactoryDataTabID = '" + modelFinDepRecvAndPayDetailTab_New.FactoryDataTabID.ToString() + "'"
                                   , ref nAccuIndex, ref dtPayTaxBenJin, ref dicPayTaxPercent2InvoiceYuE, modelFinDepRecvAndPayDetailTab_New);

                    if (bFinishBackTax)
                    {
                        return dicPayTaxPercent2InvoiceYuE;
                    }
                    else
                    {
                        bFinishBackTax = BackTaxStep(@"FinCheckInvoiceRecordDetailTab_New.BianHao > '"
                                           + strLastBianNo + @"' and FinCheckInvoiceRecordDetailAuxiliaryTab_New.IsPayTaxDone = 0 and PayTaxPercent = '" + dtBackTaxPoint
                                           + "' and FactoryDataTab.FactoryDataTabID = '" + modelFinDepRecvAndPayDetailTab_New.FactoryDataTabID.ToString() + "'"
                                           , ref nAccuIndex, ref dtPayTaxBenJin, ref dicPayTaxPercent2InvoiceYuE, modelFinDepRecvAndPayDetailTab_New);
                        return dicPayTaxPercent2InvoiceYuE;

                    }

                }
            }
        }

       

        protected virtual string ProvideRecvMoney(DGBY.Model.FinDepRecvAndPayDetailTab_New modelFinDepRecvAndPayDetailTab_New
            , ref Decimal dtThisRecv, ref Decimal dtThis)
        {
            //Decimal dtThisRecv = 0;
            DataSet dsPayAndRecv = m_bllFinDepRecvAndPayDetailTab_New.GetList("FinRecvMoneyDetailTab_New.isNotJoinBackTax = 0 and FinDepRecvAndPayDetailTab_New.RecvBianNo = '" + modelFinDepRecvAndPayDetailTab_New.RecvBianNo + "'");
            if (dsPayAndRecv.Tables.Count > 0 && dsPayAndRecv.Tables[0].Rows.Count > 0)
            {
                //dtHuiLv = Convert.ToDecimal(dsTemp.Tables[0].Rows[0]["汇率"].ToString());
                dtThisRecv = Convert.ToDecimal(dsPayAndRecv.Tables[0].Rows[0]["外汇收入"].ToString());
            }

            Decimal dtFactor = GetRecvAuxiliary2RecvFactor(modelFinDepRecvAndPayDetailTab_New.RecvBianNo);

            //在收款补充表里获取收款编号是RecvBianNo的全部付款编号
            List<string> lstPayTaxBianNo = new List<string>();
            DataSet dsRecvAuxiliary = m_bllFinRecvMoneyAuxiliaryTab_New.GetFinRecvMoneyAuxiliaryInfo(@"FinRecvMoneyDetailTab_New.isNotJoinBackTax = 0
 and FinRecvMoneyAuxiliaryTab_New.PayTaxBianHao != '' and FinRecvMoneyDetailTab_New.BianHao = '"
                + modelFinDepRecvAndPayDetailTab_New.RecvBianNo + "'");

            if (dsRecvAuxiliary.Tables.Count > 0 && dsRecvAuxiliary.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < dsRecvAuxiliary.Tables[0].Rows.Count; i++)
                {
                    string strTemp = dsRecvAuxiliary.Tables[0].Rows[i]["退税编号"].ToString();
                    if (!lstPayTaxBianNo.Contains(strTemp))
                        lstPayTaxBianNo.Add(strTemp);
                }
            }

            Decimal dtTemp = 0;
            Decimal dtTempTotal = 0;
            //Decimal dtThis = 0;
            string strGongshi = "";

            DataSet dsInvoiceAuxiliary = new DataSet();
            if (lstPayTaxBianNo.Count == 0)//这笔收款之前没有退过税
            {

            }
            else
            {
                foreach (string strPayTaxBianHao in lstPayTaxBianNo)
                {
                    dsInvoiceAuxiliary = m_bllInvoiceAuxiliary_New.GetInvoiceLastBackTaxTotal("FinCheckInvoiceRecordDetailAuxiliaryTab_New.PayTaxBianHao = '" + strPayTaxBianHao + "'");
                    if (dsInvoiceAuxiliary.Tables.Count > 0 && dsInvoiceAuxiliary.Tables[0].Rows.Count > 0)
                    {
                        for (int i = 0; i < dsInvoiceAuxiliary.Tables[0].Rows.Count; i++)
                        {
                            dtTemp = (Decimal)dsInvoiceAuxiliary.Tables[0].Rows[i]["totalPayedTaxMoneyValue"] / dtFactor;
                            dtTempTotal += dtTemp;
                            
                            strGongshi += " - " + dtTemp.ToString("0.00");
                        }
                    }
                }


            }


            dtThis = dtThisRecv - dtTempTotal;//本次剩余收款

            return strGongshi;
        }


        public virtual void ProcessRecvInvoice(ref DGBY.Model.FinDepRecvAndPayDetailTab_New modelFinDepRecvAndPayDetailTab_New
            , int nFomularSelect)
        {
            if (nFomularSelect == 0)
            {
                if (modelFinDepRecvAndPayDetailTab_New.FactorB == 0 || modelFinDepRecvAndPayDetailTab_New.FactorA == 0)
                {
                    modelFinDepRecvAndPayDetailTab_New.ToFacMoney = 0;
                    modelFinDepRecvAndPayDetailTab_New.PayTaxBenJinGongShi = "0";
                }
                else
                {
                    Decimal dtThisRecv = 0;
                    Decimal dtThis = 0;
                    string strGongshi = ProvideRecvMoney(modelFinDepRecvAndPayDetailTab_New, ref dtThisRecv, ref dtThis);

                    //以上收款已经准备好
                    Dictionary<Decimal, Decimal> dicPayTaxPercent2InvoiceYuE = ComputeComeToFacMoneyDiffBackTax(nFomularSelect, modelFinDepRecvAndPayDetailTab_New);
                    //以上包含了特殊退税点和正常退税点，审票金额 <= 退税本金

                    ComputeCometoFacFormulaForFomularSelectZero(dicPayTaxPercent2InvoiceYuE, ref modelFinDepRecvAndPayDetailTab_New
                        , dtThisRecv, dtThis, strGongshi);
                }
            }
            else
            {

                if (modelFinDepRecvAndPayDetailTab_New.FactorB == 0 || modelFinDepRecvAndPayDetailTab_New.FactorA == 0)
                {
                    modelFinDepRecvAndPayDetailTab_New.ToFacMoney = 0;
                    modelFinDepRecvAndPayDetailTab_New.PayTaxBenJinGongShi = "0";
                }
                else
                {
                    Decimal dtThisRecv = 0;
                    Decimal dtThis = 0;
                    string strGongshi = ProvideRecvMoney(modelFinDepRecvAndPayDetailTab_New, ref dtThisRecv, ref dtThis);

                    Dictionary<Decimal, Decimal> dicPayTaxPercent2InvoiceYuE = ComputeComeToFacMoneyDiffBackTax(nFomularSelect, modelFinDepRecvAndPayDetailTab_New);

                    ComputeCometoFacFormulaForFomularSelectOne(dicPayTaxPercent2InvoiceYuE, ref modelFinDepRecvAndPayDetailTab_New
                        , dtThisRecv, dtThis, strGongshi);

                }
            }
        }

    }
}
